﻿using DevExpress.Mvvm;
using DevExpress.Pdf.Native.BouncyCastle.Ocsp;
using HandyControl.Controls;
using PlaceOrder_Controls.Controls.Select;
using PlaceOrder_Core.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlaceOrder_Client.ViewModels
{
    /// <summary>
    /// 成交复盘页面
    /// </summary>
    public class TransactionReviewViewModel:BindableBase
    {
        #region 依赖属性
        private double _maxValue;
        public double MaxValue
        {
            get { return _maxValue; }
            set
            {
                _maxValue = value;
                RaisePropertiesChanged();
            }
        }

        private double _minValue;
        public double MinValue
        {
            get { return _minValue; }
            set
            {
                _minValue = value;
                RaisePropertiesChanged();
            }
        }

        private string yingKuiStatus="盈亏分析";

        public string YingKuiStatus
        {
            get { return yingKuiStatus; }
            set { yingKuiStatus = value;RaisePropertiesChanged(); }
        }


        private int nameIndexStart;

        public int NameIndexStart
        {
            get { return nameIndexStart; }
            set { nameIndexStart = value; RaisePropertiesChanged(); }
        }


        private ObservableCollection<string> TabbarData_;
        /// <summary>
        /// tabbar data（天、周、月、年）
        /// </summary>
        public ObservableCollection<string> TabbarData
        {
            get { return TabbarData_; }
            set { TabbarData_ = value; RaisePropertiesChanged(); }
        }

        private int tabbarSelectedIndex;

        /// <summary>
        /// 选中天、周、月、年
        /// </summary>
        public int TabbarSelectedIndex
        {
            get { return tabbarSelectedIndex; }
            set { tabbarSelectedIndex = value; RaisePropertiesChanged(); }
        }


        private string weekDateStr;

        public string WeekDateStr
        {
            get { return weekDateStr; }
            set { weekDateStr = value; RaisePropertiesChanged(); }
        }

        private DateTime date;
        public DateTime Date { get { return date; } set { date = value; RaisePropertiesChanged() ; } }

        private SelectItemModel selectedWeek;

        public SelectItemModel SelectedWeek { get { return selectedWeek; } set { selectedWeek = value; RaisePropertiesChanged(); } }



        private DateTime monthDate;
        public DateTime MonthDate { get { return monthDate; } set { monthDate = value; RaisePropertiesChanged(); } }

        private DateTime yearDate;
        public DateTime YearDate { get { return yearDate; } set { yearDate = value; RaisePropertiesChanged(); } }

        private SelectItemModel chartDataMode;
        /// <summary>
        /// 图表数据模式（1=分类/2=汇总）
        /// </summary>
        public SelectItemModel ChartDataMode { get { return chartDataMode; } set { chartDataMode = value; RaisePropertiesChanged(); } }

        private List<SelectItemModel> weekOptions;

        public List<SelectItemModel> WeekOptions { get { return weekOptions; } set { weekOptions = value; RaisePropertiesChanged(); } }
        #endregion

        #region 命令
        public DelegateCommand<ProfitLossData> BarClickedCommand { get; set; }
        #endregion


        public TransactionReviewViewModel()
        {
            InitValue();
            InitCommands();
            Test();
            PropertyChanged += TransactionReviewViewModel_PropertyChanged;
        }

        /// <summary>
        /// 最关键的，当绑定依赖属性变化而变化 页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TransactionReviewViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(TabbarSelectedIndex))
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (TabbarSelectedIndex == 0)
            {
                NameIndexStart = 0;

                LoadDayData();
            }
            else if (TabbarSelectedIndex == 1)
            {
                LoadWeekData();
            }
            else if (TabbarSelectedIndex == 2)
            {
                NameIndexStart = 1;

                LoadMonthlyData();
            }
            else if (TabbarSelectedIndex == 3)
            {
                LoadYearData();
            }
        }

        #region 测试数据（柱形图）

        private ObservableCollection<ProfitLossData> _testData;
        public ObservableCollection<ProfitLossData> TestData
        {
            get { return _testData; }
            set
            {
                _testData = value;
                RaisePropertiesChanged();
            }
        }
        public void Test()
        {
            TestData = new ObservableCollection<ProfitLossData>
        {

            new ProfitLossData { Date = DateTime.Now.AddDays(-8), ProfitLoss = 11, Name = "Item1", Number = 500000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-7), ProfitLoss = 14000, Name = "Item2", Number = 300000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-6), ProfitLoss = 150000, Name = "Item3", Number = 400000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-5), ProfitLoss = 50000, Name = "Item4", Number = 200000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-4), ProfitLoss = 200, Name = "Item1", Number = 500000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-3), ProfitLoss = -200000, Name = "Item2", Number = 300000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-2), ProfitLoss = 150000, Name = "Item3", Number = 400000 },
            new ProfitLossData { Date = DateTime.Now.AddDays(-1), ProfitLoss = -50000, Name = "Item4", Number = 200000 },
            new ProfitLossData { Date = DateTime.Now, ProfitLoss = 250000, Name = "Item5", Number = 600000 }
        };

            MaxValue = TestData.Max(item => item.ProfitLoss);
            MinValue = TestData.Min(item => item.ProfitLoss);
        }
        #endregion


        /// <summary>
        /// 加载年数据
        /// </summary>
        private void LoadYearData()
        {
            
        }

        /// <summary>
        /// 加载月数据
        /// </summary>
        private void LoadMonthlyData()
        {
            
        }

        /// <summary>
        /// 加载周数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LoadWeekData()
        {
           
        }

        /// <summary>
        /// 加载日数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LoadDayData()
        {
            
        }

        private void InitCommands()
        {
            BarClickedCommand = new DelegateCommand<ProfitLossData>(BarClickHandle); //单击柱体时触发
        }

        private void BarClickHandle(ProfitLossData item)
        {
            // 在这里处理柱体点击事件
            Growl.InfoGlobal($"柱体被点击: {item.Date} - {item.ProfitLoss}");

            AppLog.WriteInfo($"柱体被点击: {item.Date} - {item.ProfitLoss}");
        }

        public List<SelectItemModel> ChartDataModeOptions { get; set; }
        private void InitValue()
        {
            TabbarData = new System.Collections.ObjectModel.ObservableCollection<string>()
            {
                "按天","按周","按月","按年"
            };

            var weekOptions = new List<SelectItemModel>();
            weekOptions.Add(new SelectItemModel()
            {
                Name = "本周"
            });
            weekOptions.Add(new SelectItemModel()
            {
                Name = "上周"
            });

            var chartDataModeOptions = new List<SelectItemModel>
            {
                new SelectItemModel()
                {
                    Id = 1,
                    Name = "默认视图",
                },
                new SelectItemModel()
                {
                    Id = 2,
                    Name = "汇总视图"
                },
                new SelectItemModel()
                {
                    Id = 3,
                    Name = "分类视图"
                }
            };

            ChartDataModeOptions = chartDataModeOptions;
            ChartDataMode = chartDataModeOptions[0];
            //ShowType = ShowTypeOptions[0];
            WeekOptions = weekOptions;
            SelectedWeek = weekOptions[0];
            MonthDate = DateTime.Now;
            TabbarSelectedIndex = 0;
            Date = DateTime.Now;
            YearDate = DateTime.Now;

        }
    }

    public class ProfitLossData
    {
        public DateTime Date { get; set; }
        public double ProfitLoss { get; set; }
        public string Name { get; set; }
        public double Number { get; set; }
    }
}
